package com.wkedu.repair.controller;

import com.wkedu.repair.dto.ReachDto;
import com.wkedu.repair.dto.RepairStudentDto;
import com.wkedu.repair.dto.StudentDto;
import com.wkedu.repair.enums.CodeEnum;
import com.wkedu.repair.exception.LoginException;
import com.wkedu.repair.form.*;
import com.wkedu.repair.reponse.Response;
import com.wkedu.repair.service.ReachService;
import com.wkedu.repair.service.RepairStudentService;
import com.wkedu.repair.service.StudentService;
import com.wkedu.repair.service.TeacherService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.apache.bcel.classfile.Code;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 港港
 * @version 1.0
 * @date 2021/9/5 11:12 上午
 */
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;
    @Autowired
    private RepairStudentService repairStudentService;

    @Autowired
    private ReachService reachService;

    @Autowired
    private TeacherService teacherService;

    @PostMapping("/login")
    public Response<String> studentLogin(@RequestBody StudentLoginForm studentLoginForm) throws LoginException {
        Response<String> response = new Response<>();
        StudentDto studentDto = studentService.getStudentForUsername(studentLoginForm.getUsername());
        if (studentDto != null) {
            if (studentLoginForm.getPassword().equals(studentDto.getPassword())) {
                response.setCode(CodeEnum.SUCCESS.getCode());
            }

        }else {
            response.setCode(CodeEnum.ERROR.getCode());
            response.setData(null);


        }


        return response;
    }

    @PostMapping("/sign")
    public Response studentSign(@RequestBody StudentSignForm studentSignForm) throws LoginException {
        Response<String> response = new Response<>();


        StudentDto studentDto1 = studentService.getStudentForStudentId(studentSignForm.getStudentId());
        StudentDto studentDto2 = studentService.getStudentForUsername(studentSignForm.getUsername());

        if (studentDto1 == null && studentDto2 == null){
            StudentDto studentDto = new StudentDto();
            studentDto.setStudentId(studentSignForm.getStudentId());
            studentDto.setMajor(studentSignForm.getMajor());
            studentDto.setUsername(studentSignForm.getUsername());
            studentDto.setName(studentSignForm.getName());
            studentDto.setPassword(studentSignForm.getPassword());


            studentService.addStudent(studentDto);
            response.setCode(CodeEnum.SUCCESS.getCode());
            response.setData("用户新建成功");
        }else{
            response.setCode(CodeEnum.ERROR.getCode());
            response.setData("已有该用户");
        }


        return response;
    }

    @PostMapping("/upload")
    public Response studentUpload(HttpServletRequest request, @RequestParam("file") MultipartFile file,
                                  @RequestParam String repairInfo,
                                  @RequestParam String repairPath,
                                  @RequestParam String studentId)
            throws IOException {

        Response<String> response = new Response<>();

        String fileName = file.getOriginalFilename();
        String newFileName = fileName + "_" + repairInfo + "_" + repairPath+ "_" + studentId + ".jpg";
        // 得到文件保存的位置以及新文件名
        File dest = new File(System.getProperty("user.dir")+
                "/upload_photo/student/"+ newFileName);

        try {
            // 上传的文件被保存了
            file.transferTo(dest);
            // 打印日志
            System.out.println("上传成功，当前上传的文件保存在 {}"+ newFileName);
            // 自定义返回的统一的 JSON 格式的数据，可以直接返回这个字符串也是可以的。
            response.setCode(CodeEnum.SUCCESS.getCode());
        } catch (IOException e) {
            log.error("文件上传出错");
        }


        response.setCode(CodeEnum.SUCCESS.getCode());


        return response;
    }

    @PostMapping("/repair")
    public Response repairStudent(@RequestBody RepairStudentForm repairStudentForm){
        Response<String> response = new Response<>();

        RepairStudentDto repairStudentDto = new RepairStudentDto(
                repairStudentForm.getRepairInfo(), repairStudentForm.getRepairPath(),
                repairStudentForm.getRepairType(), repairStudentForm.getStudentId()
        );

        repairStudentService.add(repairStudentDto);

        response.setCode(CodeEnum.SUCCESS.getCode());
        response.setData("请求插入成功");

        return response;
    }

    @GetMapping("/list_repair")
    public Response listRepairStudent(@RequestParam String studentId) throws LoginException {
        Response<List<RepairStudentForm>> response = new Response<>();


        List<RepairStudentDto> repairStudentDtoList = repairStudentService.listForStudentId(studentId);

        if (!repairStudentDtoList.isEmpty()) {
            List<RepairStudentForm> repairStudentFormList = repairStudentDtoList.stream().map(
                    i -> new RepairStudentForm(
                            i.getRepairInfo(), i.getRepairPath(), i.getRepairType(), i.getStudentId()
                    )
            ).collect(Collectors.toList());

            response.setCode(CodeEnum.SUCCESS.getCode());
            response.setData(repairStudentFormList);
        }else {

            response.setData(null);
            throw new LoginException("没有报修信息");

        }

        return response;
    }


    @GetMapping("/get_student_info")
    public Response getStudentInfo(@RequestParam String username){
        Response<StudentInfoForm> response = new Response<>();
        StudentDto studentDto = studentService.getStudentForUsername(username);

        if (studentDto != null){
            StudentInfoForm studentInfoForm = new StudentInfoForm(studentDto.getName(),
                    studentDto.getStudentId(), studentDto.getUsername());

            response.setCode(CodeEnum.SUCCESS.getCode());
            response.setData(studentInfoForm);
        }else{
            response.setCode(CodeEnum.SUCCESS.getCode());
            response.setData(null);
        }

        return response;
    }


    @PostMapping("/sub_reach")
    public Response subReach(@RequestBody ReachForm reachForm){
        Response<String> response = new Response<>();

        StudentDto studentForStudentId = studentService.getStudentForStudentId(reachForm.getReachStudentId());
        if (studentForStudentId != null){
            ReachDto reachDto = new ReachDto(reachForm.getReachStudentId(), new Date(),
                    reachForm.getReachCourse(), reachForm.getReachTeacher());

            reachService.add(reachDto);

            response.setCode(CodeEnum.SUCCESS.getCode());
            response.setData("成功");

        }else {

            response.setCode(CodeEnum.ERROR.getCode());
            response.setData("没有学生");
        }



        return response;
    }


    @GetMapping("/list_reach_student")
    public Response listReachStudent(String studentId){
        Response<List<ReachDto>> response = new Response<>();

        List<ReachDto> reachDtoList = reachService.listReachForStudent(studentId);

        if (!reachDtoList.isEmpty()) {
            response.setCode(CodeEnum.SUCCESS.getCode());
            response.setData(reachDtoList);

        }else {
            response.setCode(CodeEnum.ERROR.getCode());
            response.setData(null);
        }


        return response;
    }




}
